//
// Description: 5979. 全部开花的最早一天
// Created by Loading on 2022/1/9.
//

#include <bits/stdc++.h>

using namespace std;

int earliestFullBloom(vector<int> &plantTime, vector<int> &growTime) {
    vector<pair<int, int>> vec;
    int n = plantTime.size();

    for (int i = 0; i < n; ++i) {
        vec.emplace_back(growTime[i], plantTime[i]);
    }

    /* 排序，生长周期长的靠前 */
    sort(vec.begin(), vec.end(), greater<>());

    int res = 0;
    // 当前种植总时间
    int current = 0;
    for (auto &p : vec) {
        current += p.second;
        /* 每次都至少取播种周期 + 生长周期，保证此植物开花 */
        res = max(res, current + p.first);
    }

    return res;
}

int main() {
    vector<int> plantTime = {1, 2, 3, 2};
    vector<int> growTime = {2, 1, 2, 1};
    int res = earliestFullBloom(plantTime, growTime);
    cout << res << endl;

    return 0;
}
